package com.tungse.study.io.four;

import com.tungse.study.threadpool.DiscardRejectHandle;
import com.tungse.study.threadpool.MyThreadPool;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Tungse
 * 伪异步的通信实现
 */
public class Server4 {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("server start");

        //初始一个线程池
        //HandlerSocketServerPool handlerSocketServerPool = new HandlerSocketServerPool(2, 5, 10);
        MyThreadPool handlerSocketServerPool = new MyThreadPool(2, 4,
                1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(4), new DiscardRejectHandle());

        while (true) {
            try {
                //获取客端的链接
                Socket socket = serverSocket.accept();
                //将socket包装到任务对象中
                ServerSocketRunnable task = new ServerSocketRunnable(socket);
                //将任务提交给线程池
                handlerSocketServerPool.execute(task);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
